package com.kgc.sbt.provider111;

import com.alibaba.dubbo.config.annotation.Service;
import com.kgc.sbt.api.ReceiveCouponsApi;
import com.kgc.sbt.beans.*;
import com.kgc.sbt.mapper.CouponsTypeMapper;
import com.kgc.sbt.mapper.UserCouponsMapper;
import com.kgc.sbt.mapper.UserLoginMapper;
import com.kgc.sbt.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created on 2021/8/19.
 * <p>
 * Author: songhe
 * <p>
 * Description:
 */
@Service
@Slf4j
public class receivecCouponPro implements ReceiveCouponsApi {


    @Autowired
    private CouponsTypeMapper couponsTypeMapper;

    @Autowired
    private UserCouponsMapper userCouponsMapper;

    @Autowired
    private UserLoginMapper userLoginMapper;

    @Autowired
    private RedisUtils redisUtils;


    /**
     * @return :
     * @author : songhe
     * @date : 2021/8/20
     * @description :  查询所有优惠卷类型
     */
    @Override
    public List<CouponsType> queryCouponType() {
        return couponsTypeMapper.queryAllCouponsType();
    }

    @Override
    public List<QueryCoupons> queryUserCoupons(Integer userId) {
        return userCouponsMapper.queryUserCoupons(userId);
    }

    @Override
    public String getCoupons(Integer userId, String couponsNum) {

        //获取用户的积分
        UserLoginExample userLoginExample = new UserLoginExample();
        UserLoginExample.Criteria criteria = userLoginExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        UserLogin integral = userLoginMapper.selectByExample(userLoginExample).get(0);
        log.info("integral: {}", integral);

        int point = integral.getIntegral();
        if (point < 100) {
            return "会员积分小于100，积分不足，不能领劵！";
        } else {
            //减积分
            int vipPoint = integral.getIntegral();
            integral.setIntegral(vipPoint - 100);
            userLoginMapper.updateByPrimaryKey(integral);
            //添加优惠卷详情
            UserCoupons coupons = new UserCoupons();
            coupons.setCouponsNum(couponsNum);
            Date NowTime = new Date();
            log.info("现在时间：{}", NowTime);
            coupons.setStartTime(NowTime);
            coupons.setUserId(userId);
            long EndTime = NowTime.getTime() + 1000 * 60 * 60 * 24 * 5;
            Calendar time = Calendar.getInstance();
            time.setTimeInMillis(EndTime);
            log.info("结束时间：{}", EndTime);
            long redisLimitTime = (EndTime - NowTime.getTime()) + 1;
            coupons.setEndTime(time.getTime());
            userCouponsMapper.insert(coupons);

            //获取插入优惠卷的coupons_id
            UserCouponsExample userCouponsExample = new UserCouponsExample();
//            UserCouponsExample.Criteria criteria1 =userCouponsExample.createCriteria();
            userCouponsExample.setOrderByClause(" coupons_id desc ");
            UserCoupons userCoupons = userCouponsMapper.selectByExample(userCouponsExample).get(0);
            int couponId = userCoupons.getCouponsId();
            log.info("couponId:{}", couponId);

            redisUtils.set("coupons:" + couponId, 1, 60 * 60 * 24 * 6);
            return "购买成功";
        }
    }

    @Override
    public String deleteBycouponId(Integer couponId) {
        userCouponsMapper.deleteByCouponId(couponId);
        return "删除成功";
    }

}
